/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.util.Date;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.bean.UserBean;
import model.dao.UserDAO;
import model.dto.User;
import model.util.MD5Encryption;
import model.util.SharingUtil;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author Aptech
 */
public class UserAction extends BaseAction {

    UserDAO daoUser = new UserDAO();

    /**
     * forward user to 'add' page
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        return mapping.findForward("add");
    }

    /**
     * forward user to edit page
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward edit(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int userId = Integer.parseInt(request.getParameter("id") == null ? "0" : request.getParameter("id"));
        User u = daoUser.getUser(userId);
        if (u.getId() > 0) {
            request.setAttribute("user", u);
        } else {
            SharingUtil.showMessage(request, new ActionMessage("edit.failure"));
        }
        return mapping.findForward("edit");
    }

    public ActionForward create(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        User u = new User();
        UserBean bnUser = (UserBean) form;
        u.setUsername(bnUser.getUsername());
        u.setPassword(bnUser.getPassword());
        u.setEmail(bnUser.getEmail());
        u.setFirstName(bnUser.getFirstName());
        u.setLastName(bnUser.getLastName());
        u.setGender(bnUser.getGender());
        u.setRole(bnUser.getRole());
        u.setRegisteredDate(new Date());
        if (daoUser.create(u) > 0) {
            SharingUtil.showMessage(request, new ActionMessage(MSG_CREATE_SUCCESS));
        } else {
            SharingUtil.showMessage(request, new ActionMessage(MSG_CREATE_FAILURE));
        }
        return mapping.findForward("add");
    }

    public ActionForward update(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        User u = new User();
        UserBean bnUser = (UserBean) form;
        u.setId(bnUser.getId());
        u.setUsername(bnUser.getUsername());
        u.setPassword(bnUser.getPassword());
        u.setEmail(bnUser.getEmail());
        u.setFirstName(bnUser.getFirstName());
        u.setLastName(bnUser.getLastName());
        u.setGender(bnUser.getGender());
        u.setRole(bnUser.getRole());
        u.setRegisteredDate(new Date());
        if (daoUser.create(u) > 0) {
            SharingUtil.showMessage(request, new ActionMessage(MSG_UPDATE_SUCCESS));
        } else {
            SharingUtil.showMessage(request, new ActionMessage(MSG_UPDATE_FAILURE));
        }
        return mapping.findForward("edit");

    }

    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        int userId = Integer.parseInt(request.getParameter("id") == null ? "0" : request.getParameter("id"));
        if (daoUser.delete(userId) > 0) {
            SharingUtil.showMessage(request, new ActionMessage(MSG_DELETE_SUCCESS));
        } else {
            SharingUtil.showMessage(request, new ActionMessage(MSG_DELETE_FAILURE));
        }
        return mapping.findForward("list");

    }
    
      public ActionForward authenticate(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
          MD5Encryption md5=new MD5Encryption();
        String username=request.getParameter("username")==null?"":request.getParameter("username");
        String password=request.getParameter("password")==null?"":request.getParameter("password");
        response.addCookie(new Cookie("remember",request.getParameter("remember-me")!=null?"true":"false"));
        User u=daoUser.getUserByIDAndPassword(username,md5.encrypt(password));
        if(u.getId()>0){
            request.getSession().setAttribute(SharingUtil.VAR_S_USER, u);
            request.setAttribute(CONTENT_HOLDER, "home");          
        }
        SharingUtil.showMessage(request, new ActionMessage("login.faiure"));
        request.setAttribute(CONTENT_HOLDER, "login");
        return mapping.findForward("home");

    }
}
